<template>

<div>
  <el-dialog
      width="100%"
    top="0"
    :fullscreen="true" 
    :title="!dataForm.seqno ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="150px">

    <el-row v-if="!dataForm.seqno">
      <el-col :span="24">
            <el-form-item label="网点编号" prop="branchid" >
              <el-input v-model="dataForm.branchid" placeholder="网点编号" maxlength="12"></el-input>
            </el-form-item>
      </el-col>


    </el-row>

    <el-row>

      <el-col :span="12">
          <el-form-item label="网点名称" prop="branchname">
            <el-input v-model="dataForm.branchname" placeholder="网点名称"></el-input>
          </el-form-item>
      </el-col>

      <el-col :span="12">
        <el-form-item label="备注" prop="remark">
            <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
          </el-form-item></el-col>
    </el-row>

    <el-row>

      <el-col :span="12">


          <el-form-item label="网点简称" prop="branchabbr">
            <el-input v-model="dataForm.branchabbr" placeholder="网点简称"></el-input>
          </el-form-item>
      </el-col>

      <el-col :span="12">
          <el-form-item label="法人代表名称" prop="name">
            <el-input v-model="dataForm.name" placeholder="法人代表名称"></el-input>
          </el-form-item>
      </el-col>

    </el-row>


    <el-row>
      <el-col :span="12">
          <el-form-item label="网点类型" prop="btype">
            <el-select v-model="dataForm.btype" placeholder="网点类型" style="width:100%">
                <el-option
                  v-for="item in dataForm.orgTypes"
                  :key="item.value"
                  :label="item.text"
                  :value="item.value">
                </el-option>
            </el-select>
          </el-form-item>
      </el-col>
      <el-col :span="12">
          <el-form-item label="归属参与者" prop="attechorg">
            <el-select v-model="dataForm.attechorg" placeholder="归属参与者" style="width:100%">
                <el-option
                  v-for="item in dataForm.bankList"
                  :key="item.bankid"
                  :label="item.bankname"
                  :value="item.bankid">
                </el-option>
            </el-select>
          </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="12">
        <el-form-item label="上级机构" prop="parent">
          <el-input v-model="dataForm.parent" placeholder="上级机构网点编号"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="统一社会信用代码" prop="uscc">
          <el-input v-model="dataForm.uscc" placeholder="统一社会信用代码"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="12">
          <el-form-item label="所属地市" prop="seRegion">
            <el-select v-model="dataForm.seRegion"  @change="selectChanged"  placeholder="所属区域" style="width:100%">
                <el-option
                  v-for="item in dataForm.seRegionList"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code">
                </el-option>
            </el-select>
          </el-form-item>
      </el-col>
            <el-col :span="12">
          <el-form-item label="所属区县" prop="region">
            <el-select v-model="dataForm.region" placeholder="所属区域" style="width:100%">
                <el-option
                  v-for="item in dataForm.regionList"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code">
                </el-option>
            </el-select>
          </el-form-item>
      </el-col>

    </el-row>    

    <el-row>
      <el-col :span="12">
          <el-form-item label="网点联系电话" prop="phone">
            <el-input v-model="dataForm.phone" placeholder="网点联系电话"></el-input>
          </el-form-item>
      </el-col>
      <el-col :span="12">
          <el-form-item label="机构状态" prop="state">
            <el-select v-model="dataForm.state" placeholder="机构状态" style="width:100%">
                <el-option
                  v-for="item in dataForm.orgState"
                  :key="item.value"
                  :label="item.text"
                  :value="item.value">
                </el-option>
            </el-select>
          </el-form-item>
      </el-col>
    </el-row>    

    <el-row>
      <el-col :span="12">
          <el-form-item label="启用时间" prop="enabledate">
            
            <el-date-picker
                v-model="dataForm.enabledate"
                type="datetime"
                placeholder="选择启用时间"
                align="right"
                style="width:100%"
				format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss"
                :picker-options="pickerOptions">
              </el-date-picker>

          </el-form-item>
      </el-col>
      <el-col :span="12">
          <el-form-item label="停用时间" prop="disabledate">
            <el-date-picker
                v-model="dataForm.disabledate"
                type="datetime"
                placeholder="选择停用时间"
				format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss"	
                align="right"
                style="width:100%"
                :picker-options="pickerOptions">
              </el-date-picker>        
         </el-form-item>
      </el-col>
    </el-row>   


    <el-row>
      <el-col :span="12">
        <el-form-item label="是否为抵押权人" prop="ismortg">
          <el-select v-model="dataForm.ismortg" placeholder="是否为抵押权人" style="width:100%">
                <el-option
                  v-for="item in dataForm.isMortgList"
                  :key="item.value"
                  :label="item.text"
                  :value="item.value">
                </el-option>
            </el-select>
        </el-form-item>
      </el-col>

      <el-col :span="12">
              <el-form-item label="注册地址" prop="addr">
      <el-input v-model="dataForm.addr" placeholder="注册地址"></el-input>
    </el-form-item>
      </el-col>

    </el-row>



    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="checkNeedDoubleCheck()">确定</el-button>
    </span>
  </el-dialog>

  <double-check v-if="doubleCheckVisible" ref="doubleCheck"  @refreshDataList="dataFormSubmit" ></double-check>


</div>

</template>

<script>

  import DoubleCheck from './double-check'

  export default {
    data () {

      var validateUscc = (rule, value, callback) => {
        if (!this.dataForm.id && !this.datepicker.validlegalbizLicNum(value)) {
          callback(new Error('统一社会信用代码有误'))
        } else {
          callback()
        }
      }


      return {


        pickerOptions:this.datepicker.pickerOptions,
        doubleCheckVisible: false,
        needDoubleCheck: false,
        visible: false,
        dataForm: {
          seqno: 0,
          branchid: '',
          branchname: '',
          branchabbr: '',
          btype: '',
          attechorg: '',
          region: '',
          seRegion: '',
          name: '',
          phone: '',
          state: '',
          enabledate: '',
          disabledate: '',
          lastupdate: '',
          remark: '',
          addr: '',
          uscc: '',
          parent: '',
          ismortg: '',
          orgTypes: [],
          orgState: [],
          regionList:[],
          bankList: [],
          seRegionList: [],
          isMortgList: []
        },
        dataRule: {
          branchid: [
            { required: true, message: '网点编号不能为空', trigger: 'blur' }
          ],
          branchname: [
            { required: true, message: '网点名称不能为空', trigger: 'blur' }
          ],
          branchabbr: [
            { required: true, message: '网点简称不能为空', trigger: 'blur' }
          ],
          btype: [
            { required: true, message: '网点类型:1-银行网点;2-登记中心网点;3-三方机构网点不能为空', trigger: 'blur' }
          ],
          attechorg: [
            { required: true, message: '归属参与者编号不能为空', trigger: 'blur' }
          ],
          region: [
            { required: true, message: '所属区域不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '法人代码名称不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '联系人电话不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '机构状态:1-正常;2-签退;9-注销不能为空', trigger: 'blur' }
          ],
          enabledate: [
            { required: true, message: '启用日期不能为空', trigger: 'blur' }
          ],
          disabledate: [
            { required: true, message: '停用日期不能为空', trigger: 'blur' }
          ],
          uscc:[
            { required: true, message: '统一社会信用代码不能为空', trigger: 'blur' },
             { validator: validateUscc, trigger: 'blur' }
          ],
          addr:[
            { required: true, message: '注册地址不能为空', trigger: 'blur' }
          ]
          ,
          ismortg:[
            { required: true, message: '是否为抵押权人不能为空', trigger: 'blur' }
          ],
          parent:[
            { required: true, message: '上级机构不能为空', trigger: 'blur' }
          ]
        }
      }
    },

    components: {
      DoubleCheck
    },

    methods: {
      init (id) {
        this.dataForm.seqno = id || 0
        this.visible = true

        this.$http({
          url: this.$http.adornUrl(`/sys/bdcbranchinfo/orgtype/list`),
              method: 'get',
              params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.orgTypes = data.orgType
              this.dataForm.orgState = data.orgState
              this.dataForm.seRegionList = data.regionList
              this.dataForm.bankList = data.bankList
              this.dataForm.isMortgList = data.isMortgList
              this.needDoubleCheck = data.needDoubleCheck;
            }
        })

        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.seqno) {
            this.$http({
              url: this.$http.adornUrl(`/sys/bdcbranchinfo/info/${this.dataForm.seqno}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.branchid = data.bdcBranchinfo.branchid
                this.dataForm.branchname = data.bdcBranchinfo.branchname
                this.dataForm.branchabbr = data.bdcBranchinfo.branchabbr
                this.dataForm.btype = data.bdcBranchinfo.btype
                this.dataForm.attechorg = data.bdcBranchinfo.attechorg
                this.dataForm.region = data.bdcBranchinfo.region
                this.dataForm.name = data.bdcBranchinfo.name
                this.dataForm.phone = data.bdcBranchinfo.phone
                this.dataForm.state = data.bdcBranchinfo.state
                this.dataForm.enabledate = data.bdcBranchinfo.enabledate
                this.dataForm.disabledate = data.bdcBranchinfo.disabledate
                this.dataForm.lastupdate = data.bdcBranchinfo.lastupdate
                this.dataForm.remark = data.bdcBranchinfo.remark

                this.dataForm.seRegion = data.seRegion
                this.dataForm.ismortg = data.bdcBranchinfo.ismortg
                this.dataForm.uscc = data.bdcBranchinfo.uscc
                this.dataForm.addr = data.bdcBranchinfo.addr
                this.dataForm.parent = data.bdcBranchinfo.parent
              }
            })
          }
        })
      },

      selectChanged(value){
          this.dataForm.region = '';
          this.$http({
          url: this.$http.adornUrl(`/sys/bdcregion/listByLevle`),
              method: 'get',
              params: this.$http.adornParams({'upregion':value})
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.regionList = data.list
            }
        })
      }
      ,

     checkNeedDoubleCheck(){
        if (this.needDoubleCheck){
          this.$refs['dataForm'].validate((valid) => {
              if (valid) {
                this.doubleCheckVisible = true;
                this.$nextTick(() => {
                  this.$refs.doubleCheck.init()
                })
              }
          })
        }else{
          this.dataFormSubmit ();
        }
      },

      // 表单提交
      dataFormSubmit (username, passwd, callback) {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/bdcbranchinfo/${!this.dataForm.seqno ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'seqno': this.dataForm.seqno || undefined,
                'branchid': this.dataForm.branchid,
                'branchname': this.dataForm.branchname,
                'branchabbr': this.dataForm.branchabbr,
                'btype': this.dataForm.btype,
                'attechorg': this.dataForm.attechorg,
                'region': this.dataForm.region,
                'name': this.dataForm.name,
                'phone': this.dataForm.phone,
                'state': this.dataForm.state,
                'enabledate': this.dataForm.enabledate,
                'disabledate': this.dataForm.disabledate,
                'lastupdate': this.dataForm.lastupdate,
                'remark': this.dataForm.remark,
                'parent': this.dataForm.parent,
                'uscc': this.dataForm.uscc,
                'addr': this.dataForm.addr,

                'doubleCheck': {
                  'username': username,
                  'passwd': passwd
                },

                'ismortg': this.dataForm.ismortg
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    if (callback){
                      callback();
                    }
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
